package com.tarzan.system.controller;

import com.tarzan.api.model.request.role.SysRoleBindingMenuReq;
import com.tarzan.common.core.base.BaseResponse;
import com.tarzan.common.core.exception.BusinessException;
import com.tarzan.system.service.ISysRoleMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 角色和菜单关联表 前端控制器
 * </p>
 *
 * @author liujinshan
 * @since 2020-07-09
 */
@RestController
@RequestMapping("sysRoleMenu")
@Slf4j
public class SysRoleMenuController {

    @Autowired
    private ISysRoleMenuService iSysRoleMenuService;


    /**
     * 根据角色id获取对应的权限集合
     * @param roleId
     * @return com.jinshan.happy.base.BasePaginationResponse
     * @Date 2020/10/10 11:34
     * @Author liujinshan
     **/
    @GetMapping(value = "getMenuInfoByRoleId/{roleId}")
    public BaseResponse<List<Integer>> getMenuInfoByRoleId(@PathVariable Long roleId){
        BaseResponse<List<Integer>> response = new BaseResponse<>();
        try{
            response = BaseResponse.ok(iSysRoleMenuService.getMenuInfoByRoleId(roleId));
        } catch (BusinessException e) {
            response.setErrCode(e.getCode());
            response.setErrMsg(e.getMsg());
        } catch (Exception e){
            response.setErrCode("500");
            response.setErrMsg(e.getMessage());
        }
        return response;
    }


    /**
     * 给角色分配菜单权限
     * @param sysRoleBindingMenuReq
     * @return com.jinshan.happy.base.BaseResponse
     * @Date 2020/10/12 9:16
     * @Author liujinshan
     **/
    @PostMapping(value = "insert",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse insert(@RequestBody SysRoleBindingMenuReq sysRoleBindingMenuReq){
        try{
            iSysRoleMenuService.insert(sysRoleBindingMenuReq);
        } catch (BusinessException e) {
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }

}
